package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.CityAdapter;
import com.zw.yzk.kabu.adapter.header.HeaderHelper;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerSelectCityComponent;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.CityModel;
import com.zw.yzk.kabu.presenter.SelectCityPresenter;
import com.zw.yzk.kabu.view.view.SelectCityView;
import com.zw.yzk.kabu.view.widget.SlideBar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/20.
 */

public class SelectCityActivity extends BaseActivity implements SelectCityView, CityAdapter.OnCitySelectListener {

    @Bind(R.id.layout_city_list)
    View cityListLayout;
    @Bind(R.id.city_list)
    RecyclerView cityList;
    @Bind(R.id.result)
    RecyclerView result;
    @Bind(R.id.slide_bar)
    SlideBar slideBar;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.no_search_result)
    TextView noSearchResult;

    private CityAdapter adapter;
    private CityAdapter resultAdapter;

    @Inject
    SelectCityPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_city;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.select_city))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerSelectCityComponent.builder()
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViews();
        presenter.loadCity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void loadCity(List<CityModel> list) {
        adapter = new CityAdapter(list, true);
        adapter.setOnCitySelectListener(this);
        cityList.setAdapter(adapter);

        initHeader(getIntent().getStringExtra(ActivityConstant.LOCATED_CITY));
    }

    @Override
    public void refreshSearchResult(List<CityModel> list) {
        noSearchResult.setVisibility(list.isEmpty() ? View.VISIBLE : View.GONE);
        if (resultAdapter == null) {
            resultAdapter = new CityAdapter(list, false);
            resultAdapter.setOnCitySelectListener(this);
            result.setAdapter(resultAdapter);
        } else {
            resultAdapter.refresh(list);
        }
    }

    @Override
    public List<CityModel> getCityList() {
        if (adapter == null) {
            return new ArrayList<>();
        }
        return adapter.getDataList();
    }


    @Override
    public void onCitySelected(CityModel model) {
        Intent intent = new Intent();
        intent.putExtra(ActivityConstant.SELECT_CITY_COMPLETE, model.getName());
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 初始化视图组件
     */
    private void initViews() {
        initRecyclerView();
        initSlideBar();

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    //查找城市
                    result.setVisibility(View.VISIBLE);
                    cityListLayout.setVisibility(View.GONE);
                    presenter.filterCity(s.toString());
                } else {
                    //不显示查找结果列表
                    result.setVisibility(View.GONE);
                    cityListLayout.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        cityList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        result.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    /**
     * 初始化侧边栏
     */
    private void initSlideBar() {
        slideBar.setSpells(Arrays.asList(getResources().getStringArray(R.array.city_first_spell)));
        slideBar.setOnSliderBarClickListener(new SlideBar.OnSlideBarClickListener() {
            @Override
            public int onSlideBarClicked(String s) {
                int position = adapter.getFirstSpellPosition(s);
                if (position != -1) {
                    cityList.scrollToPosition(position);
                }
                return position;
            }
        });
    }

    /**
     * 初始化header
     */
    private void initHeader(String location) {
        View header = LayoutInflater.from(this).inflate(R.layout.recycler_header_city_list, cityList, false);
        TextView currentCity = (TextView) header.findViewById(R.id.current_city);
        currentCity.setText(location);

        HotCityClickedListener listener = new HotCityClickedListener();
        header.findViewById(R.id.total).setOnClickListener(listener);
        header.findViewById(R.id.shanghai).setOnClickListener(listener);
        header.findViewById(R.id.beijing).setOnClickListener(listener);
        header.findViewById(R.id.guangzhou).setOnClickListener(listener);
        header.findViewById(R.id.shenzhen).setOnClickListener(listener);
        header.findViewById(R.id.wuhan).setOnClickListener(listener);
        header.findViewById(R.id.xian).setOnClickListener(listener);
        header.findViewById(R.id.nanjing).setOnClickListener(listener);
        header.findViewById(R.id.hangzhou).setOnClickListener(listener);

        //给RecyclerView添加头部
        HeaderHelper.createHelper(adapter).addHeader(cityList, header);
    }

    /**
     * 热门城市点击监听
     */
    private class HotCityClickedListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.total:
                case R.id.shanghai:
                case R.id.beijing:
                case R.id.guangzhou:
                case R.id.shenzhen:
                case R.id.wuhan:
                case R.id.xian:
                case R.id.nanjing:
                case R.id.hangzhou:
                    String city = ((TextView) v).getText().toString();
                    Intent intent = new Intent();
                    intent.putExtra(ActivityConstant.SELECT_CITY_COMPLETE, city);
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
    }
}
